#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool visted[2];
int main() 
{
	int n = 0;
	int ret = INT_MAX;
	vector<string> v;
	int prev1 = -1, prev2 = -1;
	cin >> n;
	v.resize(n);
	string s1, s2;
	cin >> s1>>s2;
	if (s1.empty() || s2.empty()) 
	{
		cout << -1;
		return 0;
	}
	for (int i = 0; i < n; i++) 
	{
		cin >> v[i];
	}
	for (int i = 0; i < n; i++) 
	{
		if (v[i] == s1) 
		{
			if (prev2 != -1) 
			{
				ret = min(ret, i - prev2);
				
			}
			prev1 = i;
		}
		else if (v[i] == s2) 
		{
			if (prev1 != -1) 
			{
				ret = min(ret, i - prev1);
			
			}
			prev2 = i;
		}
	}
	if (ret == INT_MAX) cout << -1;
	cout << ret;
	
	return 0;
}